package com.xinfu.newproject.adapter.home_adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.ElectricityRefillActivity;
import com.xinfu.newproject.activity.HomeActivity;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.activity.PaymentRecordsActivity;
import com.xinfu.newproject.activity.ResidentCertificationActivity;
import com.xinfu.newproject.activity.VisitorInvitationActivity;
import com.xinfu.newproject.activity.VisitorsRecordActivity;
import com.xinfu.newproject.activity.WaterRefillActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.home.HomeAdapterBean;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;

import java.util.List;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    //布局标识集合
    private final List<Integer> typeList;

    //设置常量
    private static final int TYPE_IMG_THREE = 2;
    private static final int TYPE_IMG_RECYCLER = 3;
    private static final int TYPE_IMG_RECYCLER_ONE = 1;
    private static final int TYPE_IMG_RECYCLER_FOUR = 4;


    private List<HomeAdapterBean> homeAdapterBean;

    //上下文和数据
    Context context;
//    ArrayList<HomeAdapterBean> list;

    //构造方法
    public HomeAdapter(Context context, List<HomeAdapterBean> homeAdapterBean,List<Integer> typeList) {
        this.context = context;
        this.homeAdapterBean = homeAdapterBean;
        this.typeList = typeList;
    }

    //添加数据
    public void addbean(List<HomeAdapterBean> list) {
        list.addAll(list);
        //更新数据
        notifyDataSetChanged();
    }


    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_IMG_RECYCLER_ONE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_adapter, parent, false);
            RecyclerViewHolder threeViewHolder = new RecyclerViewHolder(view);
            return threeViewHolder;
        } else if (viewType == TYPE_IMG_THREE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_two_adapter, parent, false);
            RecycleViewHolderOne recycleViewHolderOne = new RecycleViewHolderOne(view);
            return recycleViewHolderOne;
        } /*else if (viewType == TYPE_IMG_RECYCLER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_two_adapter, parent, false);
            RecycleViewHolderTwo recycleViewHolderTwo = new RecycleViewHolderTwo(view);
            return recycleViewHolderTwo;
        }*/
        return null;

      }

    //填充onCreateViewHolder方法返回的holder中的控件(当一个View需要出现在屏幕上时，该方法会被回调，我们需要再该方法中根据数据来更改视图)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecyclerViewHolder) {
            setFloorOne((RecyclerViewHolder) holder, position);
        }
        else if (holder instanceof RecycleViewHolderOne) {
            setFloorTwo((RecycleViewHolderOne) holder,position);
        }
//        else if (holder instanceof RecycleViewHolderTwo) {
//            setFloorThree((RecycleViewHolderTwo) holder,position);
//        }
    }

//    private void setFloorThree(RecycleViewHolderTwo holder, int position) {
//        holder.textView.setText(homeAdapterBean.get(position).getImgText());
//    }

    private void setFloorTwo(RecycleViewHolderOne holder,int position) {
        holder.textView.setText(homeAdapterBean.get(position).getImgText());
        Glide.with(context).load(homeAdapterBean.get(position).getImgView()).into(holder.imageView);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (position) {
                    case 0:
                        if (isLogin()) {
                            ((HomeActivity) context).startIntentActivity(PaymentRecordsActivity.class, null);
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case 1:
                        if (isLogin()) {
                            ((HomeActivity) context).startIntentActivity(VisitorsRecordActivity.class, null);
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case 2:
                        if (isLogin()) {
                            ((HomeActivity) context).startIntentActivity(VisitorInvitationActivity.class, null);
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case 3:
                        if (isLogin()) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("type", -1);
                            ((HomeActivity) context).startIntentActivity(ResidentCertificationActivity.class, bundle);
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                }
            }
        });
    }

    private void setFloorOne(RecyclerViewHolder holder, int position) {
        holder.itemTextview.setText(homeAdapterBean.get(position).getImgText());
        Glide.with(context).load(homeAdapterBean.get(position).getImgView()).into(holder.itemImageView);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (position) {
                    case 0:
                        if (isLogin()) {
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "水");
                            ((HomeActivity) context).startIntentActivity(WaterRefillActivity.class, bundle);
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case 1:
                        if (isLogin()) {
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "电");
                            ((HomeActivity) context).startIntentActivity(ElectricityRefillActivity.class, bundle);
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case 2:
                        if (isLogin()) {
                            ((HomeActivity) context).startIntentActivity(VisitorInvitationActivity.class, null);
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                    case 3:
                        if (isLogin()) {
                            Toast.makeText(context, "暂不支持该功能", Toast.LENGTH_SHORT).show();
                        } else {
                            ((HomeActivity) context).startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                }
            }
        });
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(context, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(context, "请先登录");
            return false;
        }

        return true;
    }



    @Override
    public int getItemCount() {
        return homeAdapterBean == null ? 0 : homeAdapterBean.size();
    }
    //还需要重写方法
    // 对应布局类型
    @Override
    public int getItemViewType(int position) {
        if (typeList.get(0) == 1) {
            return TYPE_IMG_RECYCLER_ONE;
        } else if (typeList.get(0) == 2) {
            return TYPE_IMG_THREE;
        }
        return 0;
    }


    //横向的RecyclerView
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout linearLayout;
        public ImageView itemImageView;
        public TextView itemTextview;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            itemTextview = itemView.findViewById(R.id.item_home_adapter_tv);
            itemImageView = itemView.findViewById(R.id.item_home_adapter_iv);
            linearLayout = itemView.findViewById(R.id.item_home_adapter_ll);
        }
    }


    public class RecycleViewHolderOne extends RecyclerView.ViewHolder {

        public LinearLayout linearLayout;
        public ImageView imageView;
        public TextView textView;
        public RecycleViewHolderOne(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_user_two_iv);
            textView = itemView.findViewById(R.id.item_user_two_tv);
            linearLayout = itemView.findViewById(R.id.item_user_two_adapter_ll);
        }
    }

//    public class RecycleViewHolderTwo extends RecyclerView.ViewHolder {
//
//        public TextView textView;
//        public RecycleViewHolderTwo(View itemView) {
//            super(itemView);
//            textView = itemView.findViewById(R.id.item_pop_text);
//        }
//    }
}

